package com.fast.controller.juc;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;

@RestController
@RequestMapping("/prod")
@Api(tags = "生产问题场景模拟")
@Slf4j
public class ProdQuestionController {

    @GetMapping("/jvm-cpu")
    @ApiOperation("模拟cpu飙高的问题")
    public void cpuTest() {
        while (true) {
        }
    }

    @GetMapping("/jvm-memory")
    @ApiOperation("模拟内存溢出的问题")
    public void memoryTest() {
        while (true) {
            Object o = new Object();
        }
    }

    @GetMapping("/jvm-io")
    @ApiOperation("模拟io过高的问题")
    public void ioTest() throws Exception {
        while (true) {
            File file = new File("/tmp/iotest/" + System.currentTimeMillis() + "txt");
            try (FileOutputStream outputStream = new FileOutputStream(file)) {
                for (int i = 0; i < 10000; i++) {
                    outputStream.write(new byte[256]);
                }
            }
        }
    }
}
